package me.liekkas.notify.email;

import freemarker.template.Template;
import freemarker.template.TemplateException;
import me.liekkas.notify.enums.LanguageEnum;
import me.liekkas.notify.sms.BizTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class MntMailSender extends AbstractEmailSender {
    @Autowired
    private Environment env;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    protected void init() {
        Map<BizTypeEnum, IEmailTemplate> map = new HashMap<>();
        map.put(BizTypeEnum.注册验证码, EmailTemplateEnum.邮箱注册激活验证);
        map.put(BizTypeEnum.审核状态通知, EmailTemplateEnum.审核状态通知);
        map.put(BizTypeEnum.订单已确认, EmailTemplateEnum.订单已确认);
        super.templateMap = map;
    }

    @Override
    protected MailResponse doSend(MailRequest request) {
        final Map<BizTypeEnum, IEmailTemplate> templateMap = getTemplateMap();
        if (templateMap == null || templateMap.isEmpty()) {
            return MailResponse.fail("邮件模板未初始化");
        }
        final IEmailTemplate template = templateMap.get(request.getBizTypeEnum());
        if (template == null) {
            return MailResponse.fail("无法匹配邮件模板");
        }
        final LanguageEnum languageEnum = request.getLanguageEnum();
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            helper.setFrom(env.getProperty("spring.mail.username"));
            helper.setTo(request.getToAddress());

            helper.setSubject(request.getSubject());
            helper.setText(render(template.toTemplateString(languageEnum), request.getParams()), true);
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        javaMailSender.send(mimeMessage);
        return MailResponse.success();
    }

    @Override
    protected void log(MailRequest request, MailResponse response) {

    }

    private String render(String templateName, Map<String, Object> model) throws IOException, TemplateException {
        Template t = freeMarkerConfigurer.getConfiguration().getTemplate(templateName, "UTF-8");
        t.setOutputEncoding("UTF-8");
        return FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
    }
}
